package android.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.location.Location;
import android.location.LocationManager;
import android.model.MyLocation;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class gpsmain extends Activity {
    

	//kreiranje na konstanti zaradi polesno manipuliranje so podatocite
	private static final double EARTH_RADIUS_KM = 6370.97327862;
	private static final int CANCEL_DIALOG = 0;
	private static final int BANK_DIALOG = 1;
	private static final int CHEMIST_DIALOG = 2;
	private static final int SHOP_DIALOG = 3;
	private static final int FUN_DIALOG = 4;
	private static final int BANK_DIALOG2 = 5;
	private static final int CHEMIST_DIALOG2 = 6;
	private static final int SHOP_DIALOG2 = 7;
	private static final int FUN_DIALOG2 = 8;
	
	//kreiranje niza od stringovi za prikaz vo dijalog
	CharSequence[] items = {"Show words for this section.", "Show dialogues for this section."};
//	private static final int PROGRESS_DIALOG = 0;
//	ProgressDialog progressDialog;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //se zema id-to na kopce za povrzuvanje so nastan
        Button btnManually=(Button)findViewById(R.id.btnManually);
        btnManually.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				//kreiranje na intent, odnosno se startuva novo activity
				Intent intent = new Intent(v.getContext(),ShowLocations.class);
				v.getContext().startActivity(intent);
			}
		});
        
        //se zema id-to na kopce za povrzuvanje so nastan
        Button btnGPS=(Button)findViewById(R.id.btnGPS);
        btnGPS.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
						
				//rabota so lokaciski menadzer za naoganje
				//na momentalnite koordinati na korisnikot
				LocationManager lm;
		        String context=Context.LOCATION_SERVICE;
		        lm=(LocationManager)getSystemService(context);
		        
		        String provider=LocationManager.GPS_PROVIDER;
		        Location loc=lm.getLastKnownLocation(provider);
		        //otkako ke se najdat koordinatite se startuva ovaa funkcija
		        //no moze i da ne se najdat potrebnite koordinati
		        updateWithNewLocation(loc);

			
			}
		});
    }

    /*
     * so veke vneseni koordinati za odredeni lokacii se vrsi presmetuvanje za toa koja od vnesenite
     * lokacii e najblisku do korisnikot
     */
	private void updateWithNewLocation(Location location)
    {
    	
    	//gps lokacija za bankata
    	MyLocation bank = new MyLocation();
    	
    	//gps lokacija za aptekata
    	MyLocation chemist = new MyLocation();
    	
    	//gps lokacija za pazaruvanje
    	MyLocation ramstore = new MyLocation();
    	MyLocation trgovski = new MyLocation();
    	
    	//gps lokacija za zabava
    	MyLocation dramski = new MyLocation();
    	MyLocation mnt = new MyLocation();
    	MyLocation uni_hall = new MyLocation();
    	MyLocation cinema = new MyLocation();
    	
    	//vnesuvanje na koordinatite za prethodno definiranite lokacii
    	bank.setLatit(41.992824);
    	bank.setLongit(21.425280);
    	
    	chemist.setLatit(41.994394);
    	chemist.setLongit(21.431140);
    	
    	ramstore.setLatit(41.991715);
    	ramstore.setLongit(21.427459);
    	
    	trgovski.setLatit(41.995099);
    	trgovski.setLongit(21.432938);
    	
    	dramski.setLatit(42.002316);
    	dramski.setLongit(21.407729);
    	
    	mnt.setLatit(41.994258);
    	mnt.setLongit(21.425114);
    	
    	uni_hall.setLatit(41.999483);
    	uni_hall.setLongit(21.416556);
    	
    	cinema.setLatit(41.992110);
    	cinema.setLongit(21.426617);
    	
    	if(location!=null)
    	{
    		//od location se zemaat momentalnite koordinati na korisnikot
    		double lat=location.getLatitude();
    		double lng=location.getLongitude();
    //	double lat=42.004883;
    //	double lng=21.408325;

    		//se vrsi presmetka na oddalecenosta od bankata so momentalnata lokacija
    		double bankKM=distanceBetweenLocations(lat, lng, bank.getLatit(), bank.getLondit());
    		
    		//se vrsi presmetka na oddalecenosta od aptekata so momentalnata lokacija
    		double chemistKM=distanceBetweenLocations(lat, lng, chemist.getLatit(), chemist.getLondit());
    		
    		//se vrsi presmetka na oddalecenosta od centrite za pazaruvanje so momentalnata lokacija
    		double ramstoreKM=distanceBetweenLocations(lat, lng, ramstore.getLatit(), ramstore.getLondit());
    		double trgovskiKM=distanceBetweenLocations(lat, lng, trgovski.getLatit(), trgovski.getLondit());
    		
    		//se vrsi presmetka na oddalecenosta od centrite za zabava so momentalnata lokacija
    		double dramskiKM=distanceBetweenLocations(lat, lng, dramski.getLatit(), dramski.getLondit());
    		double mntKM=distanceBetweenLocations(lat, lng, mnt.getLatit(), mnt.getLondit());
    		double uni_hallKM=distanceBetweenLocations(lat, lng, uni_hall.getLatit(), uni_hall.getLondit());
    		double cinemaKM=distanceBetweenLocations(lat, lng, cinema.getLatit(), cinema.getLondit());
    		
    		//se naoga minimalnoto rastojanie
    		double minDistance=Math.min(bankKM, chemistKM);
    		double minDistance2=Math.min(minDistance, ramstoreKM);
    		double minDistance3=Math.min(minDistance2, trgovskiKM);
    		double minDistance4=Math.min(minDistance3, dramskiKM);
    		double minDistance5=Math.min(minDistance4, mntKM);
    		double minDistance6=Math.min(minDistance5, uni_hallKM);
    		double minDistance7=Math.min(minDistance6, cinemaKM);
    		
    		//otkako ke se najde vo zavisnost od toa koja lokacija od dadenite
    		//e najblisku toj dijalog se otvora
    		if(minDistance7==bankKM)
    		{
    			showDialog(BANK_DIALOG);
    		}
    		else if(minDistance7==chemistKM)
    		{
    			showDialog(CHEMIST_DIALOG);
    		}
    		else if(minDistance7==ramstoreKM || minDistance7==trgovskiKM)
    		{
    			showDialog(SHOP_DIALOG);
    		}
    		else if(minDistance7==dramskiKM || minDistance7==mntKM || minDistance7==uni_hallKM || minDistance7==cinemaKM)
    		{
    			showDialog(FUN_DIALOG);
    		} 		 	
    	}
    	else
    	{		
    		showDialog(CANCEL_DIALOG);
    	}	
    }
    
	/*
	 * metod za presmetuvanje na rastojanie pomegu dve lokacii so dadeni koordinati
	 * go vraka rastojanieto vo km
	 */
    private double distanceBetweenLocations(double lat1, double lng1, double lat2, double lng2)
	 {
			
		//se vrsi konvertiranje vo radijani: rad=(stepeni/180)*PI
		lat1=(lat1/180)*Math.PI;
		lng1=(lng1/180)*Math.PI;
		lat2=(lat2/180)*Math.PI;
		lng2=(lng2/180)*Math.PI;
		
		//se presmetuva centralniot periferen agol
		double delta=((2*Math.asin(Math.sqrt(Math.pow((Math.sin(lat1-lat2)/2), 2)
				+Math.cos(lat1)*Math.cos(lat2)*Math.pow((Math.sin(lng1-lng2)/2), 2)))));
		
		//rastojanieto vo kilometri
		double distanceKM=delta*EARTH_RADIUS_KM;
		
		return distanceKM;		 
	 }
 
	@Override
	protected void onResume() {
		
		super.onResume();
	}

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreateDialog(int)
	 * kreiranje na dijalozi
	 * so pomos na switch se kreiraat poveke dijalozi i vo zavisnost od toa so koe id ke 
	 * se sovpadne pri baranje za startuvanje na dijalog ke se startuva dijalogot cie 
	 * id se sovpaga so baranoto
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
		
		switch(id)
		{
		case CANCEL_DIALOG:
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.cancel_icon2)
			.setTitle("Warning!")
			.setMessage("No location found, please find your location manually!")
			.setNegativeButton("OK", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					dialog.cancel();					
				}
			}).create();
			
		case BANK_DIALOG:
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.bank)
			.setTitle("Bank")
			.setMessage("GPS found that your current location is near bank. \nDo you want to open section for bank?")
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					showDialog(BANK_DIALOG2);
					
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					dialog.cancel();					
				}
			}).create();
			
		case CHEMIST_DIALOG:
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.chemist)
			.setTitle("Chemist")
			.setMessage("GPS found that your current location is near chemist. \nDo you want to open section for chemist?")
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					showDialog(CHEMIST_DIALOG2);
					
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					dialog.cancel();					
				}
			}).create();
			
		case SHOP_DIALOG:
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.shop)
			.setTitle("Shopping")
			.setMessage("GPS found that your current location is near shopping center. \nDo you want to open section for shopping?")
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					showDialog(SHOP_DIALOG2);
					
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					dialog.cancel();					
				}
			}).create();
			
		case FUN_DIALOG:
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.fun)
			.setTitle("Sport & Entertainment")
			.setMessage("GPS found that your current location is near cinema, theatre or sports center. \nDo you want to open section for sport & entertainment?")
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					showDialog(FUN_DIALOG2);
					
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					dialog.cancel();					
				}
			}).create();
			
		case BANK_DIALOG2:
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.bank)
			.setTitle("Bank")
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					dialog.cancel();					
				}
			})
			.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					if(items[which].equals("Show words for this section."))
					{
						Intent intent= new Intent(getBaseContext(), android.activity.Bank.class);
						startActivity(intent);
					}
					else
					{
						Intent intentD= new Intent(getBaseContext(), android.activity.BankDialogue.class);
						startActivity(intentD);
					}
				}
			}).create();
			
		case CHEMIST_DIALOG2:
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.chemist)
			.setTitle("Chemist")
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					dialog.cancel();					
				}
			})
			.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					if(items[which].equals("Show words for this section."))
					{
						Intent intent= new Intent(getBaseContext(), android.activity.Chemist.class);
						startActivity(intent);
					}
					else
					{
						Intent intentD= new Intent(getBaseContext(), android.activity.ChemistDialogue.class);
						startActivity(intentD);
					}
				}
			}).create();
			
		case SHOP_DIALOG2:
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.shop)
			.setTitle("Shopping")
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					dialog.cancel();					
				}
			})
			.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					if(items[which].equals("Show words for this section."))
					{
						Intent intent= new Intent(getBaseContext(), android.activity.Shop.class);
						startActivity(intent);
					}
					else
					{
						Intent intentD= new Intent(getBaseContext(), android.activity.ShopDialogue.class);
						startActivity(intentD);
					}
				}
			}).create();
			
		case FUN_DIALOG2:
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.fun)
			.setTitle("Sport & Entertainment")
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					dialog.cancel();					
				}
			})
			.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					if(items[which].equals("Show words for this section."))
					{
						Intent intent= new Intent(getBaseContext(), android.activity.Fun.class);
						startActivity(intent);
					}
					else
					{
						Intent intentD= new Intent(getBaseContext(), android.activity.FunDialogue.class);
						startActivity(intentD);
					}
				}
			}).create();
		}
		return null;
	}

}